package com.maxsalad.uc.dao;

import com.maxsalad.common.dao.BaseDao;
import com.maxsalad.uc.pojo.TPlatformEntity;
import com.maxsalad.uc.pojo.TShopEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-15.
 */
@Repository
public class ShopDao extends BaseDao<TShopEntity> {
    private void baseSearchParam(TShopEntity shopEntity, StringBuffer hql) {
        if (shopEntity == null) {
            return;
        }
        if (shopEntity.getId() != 0) {
            hql.append(" and t.id =:id");
        }

        //店铺编码
        if (StringUtils.isNotBlank(shopEntity.getShopCode())) {
            hql.append(" and t.shopCode =:shopCode");
        }
        //店铺名称
        if (StringUtils.isNotEmpty(shopEntity.getShopName())) {
            hql.append(" and t.shopName like :shopName");
            shopEntity.setShopName("%" + shopEntity.getShopName() + "%");
        }
        //description
        if (StringUtils.isNotEmpty(shopEntity.getDescription())) {
            hql.append(" and t.description like :description");
            shopEntity.setDescription("%" + shopEntity.getDescription() + "%");
        }

    }

    /**
     * 查询所有平台信息
     *
     * @param shopEntity
     * @return
     * @throws Exception
     */
    public Map queryAllPlatform(TShopEntity shopEntity) throws Exception {
        StringBuffer hql = new StringBuffer("from TShopEntity t where 1=1 ");
        this.baseSearchParam(shopEntity, hql);
        hql.append(" order by t.createTime desc ");
        return this.pageQueryByHql(hql.toString(), shopEntity);
    }

    /**
     * 查询所有店铺信息(不分页)
     *
     * @param shopEntity
     * @return
     * @throws Exception
     */
    public List<TShopEntity> queryAllPlatformNoPage(TShopEntity shopEntity) throws Exception {
        StringBuffer hql = new StringBuffer("from TShopEntity t where 1=1 ");
        this.baseSearchParam(shopEntity, hql);
        hql.append(" order by t.createTime desc ");
        return this.findByHql(hql.toString(), shopEntity);
    }
}
